package com.sg.service.biz.framework.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.sg.common.util.RedisUtil;
import com.sg.dto.base.framework.req.*;
import com.sg.dto.biz.framework.req.*;
import com.sg.dto.biz.framework.res.*;
import com.sg.entity.BehaviorType;
import com.sg.entity.BizAppScene;
import com.sg.entity.BizScene;
import com.sg.service.base.framework.MBehaviorTypeService;
import com.sg.service.base.framework.MBizAppSceneService;
import com.sg.service.base.framework.MBizSceneService;
import com.sg.common.exception.Assert;
import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Tags;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author ：
 * @version 1.0
 * @date ：
 */
@Service
public class BehaviorTypeServiceImpl
        implements com.wicket.okrframework.biz.service.BehaviorTypeService {

    @Resource
    private RedisUtil redisUtil;


    @Resource
    private MBehaviorTypeService mBehaviorTypeService;
    @Resource
    private MBizAppSceneService mBizAppSceneService;
    @Resource
    private MBizSceneService mBizSceneService;

    /**
     * D1-1新增行为类型[716]
     * gen by moon at 9/1/2022, 7:20:43 PM
     */
    @Trace(operationName = "D1-1新增行为类型")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Transactional(rollbackFor = Exception.class)
    @Override
    public AddBehaviorTypeRespDto addBehaviorType(AddBehaviorTypeReqDto reqDto) {


        String string_1 = null;
//步骤0: 1-1-10新增行为类型 - addBehaviorType
        String string = null;
        BehaviorType behaviorType = new BehaviorType();
        if (reqDto != null) {
            behaviorType.setBehaviorTypeCode(reqDto.getBehaviorTypeCode());//sourceId:27998_1
            behaviorType.setBehaviorTypeName(reqDto.getBehaviorTypeName());//sourceId:27999_1
            behaviorType.setBehaviorTypeDes(reqDto.getBehaviorTypeDes());//sourceId:28000_1
            behaviorType.setBizAppId(reqDto.getBizAppId());//sourceId:28001_1
            behaviorType.setBizSceneId(reqDto.getBizSceneId());//sourceId:286049_1
            behaviorType.setSubjectLifeCycle(reqDto.getSubjectLifeCycle());//sourceId:28002_1
        }

        /*1-1-10新增行为类型[722]   */
        Assert.isNull(behaviorType.getBehaviorTypeCode(), "D1-1新增行为类型-1-1-10新增行为类型-行为类型编码不能为空", false);
        Assert.isNull(behaviorType.getBehaviorTypeName(), "D1-1新增行为类型-1-1-10新增行为类型-行为类型名称不能为空", false);
        Assert.isNull(behaviorType.getBizAppId(), "D1-1新增行为类型-1-1-10新增行为类型-冗余业务应用ID不能为空", false);
        Assert.isNull(behaviorType.getBizSceneId(), "D1-1新增行为类型-1-1-10新增行为类型-关联业务场景ID不能为空", false);
        Assert.isNull(behaviorType.getSubjectLifeCycle(), "D1-1新增行为类型-1-1-10新增行为类型-主体生命周期不能为空", false);
        string = mBehaviorTypeService.addBehaviorType(behaviorType);


        string_1 = string;

        AddBehaviorTypeRespDto retData = new AddBehaviorTypeRespDto();
        if (string_1 != null) {
            retData.setBehaviorTypeId(string_1);//sourceId:28014_1
        }


        return retData;
    }

    /**
     * D1-1查询行为类型列表[719]
     * gen by moon at 10/24/2022, 4:14:51 AM
     */
    @Trace(operationName = "D1-1查询行为类型列表")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Transactional(rollbackFor = Exception.class)
    @Override
    public QueryBehaviorTypeListRespDto queryBehaviorTypeList(QueryBehaviorTypeListReqDto reqDto) {


        List<BehaviorType> listBehaviorType_1 = new ArrayList<>();
        List<BizAppScene> listBizAppScene_1 = new ArrayList<>();
//步骤0: 1-1-10查询行为类型列表 - queryBehaviorTypeList
        List<BehaviorType> listBehaviorType = new ArrayList<>();
        QueryBehaviorTypeListReq queryBehaviorTypeListReq = new QueryBehaviorTypeListReq();
        if (reqDto != null) {
            queryBehaviorTypeListReq.setBehaviorTypeName(reqDto.getBehaviorTypeName());//SimpleFieldAssign//sourceId:27909_1
            queryBehaviorTypeListReq.setBizAppId(reqDto.getBizAppId());//SimpleFieldAssign//sourceId:34657_1
            queryBehaviorTypeListReq.setBizSceneId(reqDto.getBizSceneId());//SimpleFieldAssign//sourceId:387993_1
            queryBehaviorTypeListReq.setIsArchive(reqDto.getIsArchive());//SimpleFieldAssign//sourceId:27911_1
            queryBehaviorTypeListReq.setSubjectLifeCycle(reqDto.getSubjectLifeCycle());//SimpleFieldAssign//sourceId:27910_1
        }

        /*1-1-10查询行为类型列表[4]   */
        Assert.isNull(queryBehaviorTypeListReq.getBizSceneId(), "D1-1查询行为类型列表-1-1-10查询行为类型列表-关联业务场景ID不能为空", false);
        listBehaviorType = mBehaviorTypeService.queryBehaviorTypeList(queryBehaviorTypeListReq);


        listBehaviorType_1 = listBehaviorType;

//步骤1: 1-1-07批量查询业务应用列表 - batchQueryBizAppScene
        List<BizAppScene> listBizAppScene = new ArrayList<>();
        if (listBehaviorType != null && !CollectionUtil.isEmpty(listBehaviorType) && listBehaviorType.size() > 0 && listBehaviorType != null && !CollectionUtil.isEmpty(listBehaviorType)) {
            BatchQueryBizAppSceneReq batchQueryBizAppSceneReq = new BatchQueryBizAppSceneReq();
            batchQueryBizAppSceneReq.setBizAppSceneList(listBehaviorType.stream().map(item -> item.getBizAppId())
                    .collect(Collectors.toList()));/*list-to-strings*///sourceId:34653_1

            /*1-1-07批量查询业务应用列表[1216]   */

            listBizAppScene = mBizAppSceneService.batchQueryBizAppScene(batchQueryBizAppSceneReq);


            listBizAppScene_1 = listBizAppScene;
        }

        QueryBehaviorTypeListRespDto retData = new QueryBehaviorTypeListRespDto();
        //数据集融合  MoreListToOneSource
        if (listBehaviorType_1 != null && !CollectionUtil.isEmpty(listBehaviorType_1) && !CollectionUtil.isEmpty(listBehaviorType_1)) {
            for (BehaviorType behaviorType : listBehaviorType_1) {
                BehaviorTypeDto retElm = new BehaviorTypeDto();
                retElm.setBehaviorTypeId(behaviorType.getBehaviorTypeId());//SimpleFieldAssign//sourceId:11946_2
                retElm.setBehaviorTypeCode(behaviorType.getBehaviorTypeCode());//SimpleFieldAssign//sourceId:11947_2
                retElm.setBehaviorTypeName(behaviorType.getBehaviorTypeName());//SimpleFieldAssign//sourceId:11948_2
                retElm.setSubjectLifeCycle(behaviorType.getSubjectLifeCycle());//SimpleFieldAssign//sourceId:11951_2
                retElm.setOperationInductionId(behaviorType.getOperationInductionId());//SimpleFieldAssign//sourceId:11952_2
                retElm.setOperateTime(behaviorType.getOperateTime());//SimpleFieldAssign//sourceId:11953_2
                retElm.setBizAppId(behaviorType.getBizAppId());//SimpleFieldAssign//sourceId:11950_2
                retData.getBehaviorTypeList().add(retElm);
                if (listBizAppScene_1 != null && !CollectionUtil.isEmpty(listBizAppScene_1)) {
                    for (BizAppScene bizAppScene : listBizAppScene_1) {
                        if (bizAppScene.getBizAppId().equals(behaviorType.getBizAppId())) {
                            retElm.setBizAppCode(bizAppScene.getBizAppCode());//SimpleFieldAssign//sourceId:15621_2
                            retElm.setBizAppName(bizAppScene.getBizAppName());//SimpleFieldAssign//sourceId:15622_2
                        }
                    }
                }

            }
        }//sourceId:27918_1


        return retData;
    }

    /**
     * D1-1查询行为类型详情[720]
     * gen by moon at 10/28/2022, 8:45:40 PM
     */
    @Trace(operationName = "D1-1查询行为类型详情")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Transactional(rollbackFor = Exception.class)
    @Override
    public QueryBehaviorTypeDetailRespDto queryBehaviorTypeDetail(QueryBehaviorTypeDetailReqDto reqDto) {


        BehaviorType behaviorType_1 = null;
        BizAppScene bizAppScene_1 = null;
        BizScene bizScene_1 = null;
//步骤0: 1-1-10查询行为类型详情 - queryBehaviorTypeDetail
        BehaviorType behaviorType = null;
        QueryBehaviorTypeDetailReq queryBehaviorTypeDetailReq = new QueryBehaviorTypeDetailReq();
        if (reqDto != null) {
            queryBehaviorTypeDetailReq.setBehaviorTypeId(reqDto.getBehaviorTypeId());//SimpleFieldAssign//sourceId:27950_1
            queryBehaviorTypeDetailReq.setBehaviorTypeCode(reqDto.getBehaviorTypeCode());//SimpleFieldAssign//sourceId:34674_1
            queryBehaviorTypeDetailReq.setSubjectLifeCycle(reqDto.getSubjectLifeCycle());//SimpleFieldAssign//sourceId:34675_1
            queryBehaviorTypeDetailReq.setIsArchive(reqDto.getIsArchive());//SimpleFieldAssign//sourceId:34676_1
        }

        /*1-1-10查询行为类型详情[156]   */

        behaviorType = mBehaviorTypeService.queryBehaviorTypeDetail(queryBehaviorTypeDetailReq);


        behaviorType_1 = behaviorType;

//步骤1: 1-1-07查询业务应用详情 - queryBizAppSceneDetail
        BizAppScene bizAppScene = null;
        if (behaviorType != null && behaviorType != null && behaviorType.getBizAppId() != null) {
            QueryBizAppSceneDetailReq queryBizAppSceneDetailReq = new QueryBizAppSceneDetailReq();
            if (behaviorType != null) {
                queryBizAppSceneDetailReq.setBizAppId(behaviorType.getBizAppId());//SimpleFieldAssign//sourceId:34677_1
            }

            /*1-1-07查询业务应用详情[32]   */
            Assert.isNull(queryBizAppSceneDetailReq.getBizAppId(), "D1-1查询行为类型详情-1-1-07查询业务应用详情-业务应用ID不能为空", false);
            bizAppScene = mBizAppSceneService.queryBizAppSceneDetail(queryBizAppSceneDetailReq);


            bizAppScene_1 = bizAppScene;
        }

//步骤2: 1-1-09查询业务场景详情 - queryBizSceneDetail
        BizScene bizScene = null;
        if (behaviorType != null && behaviorType != null && behaviorType.getBizSceneId() != null) {
            QueryBizSceneDetailReq queryBizSceneDetailReq = new QueryBizSceneDetailReq();
            if (behaviorType != null) {
                queryBizSceneDetailReq.setBizSceneId(behaviorType.getBizSceneId());//SimpleFieldAssign//sourceId:286060_1
            }

            /*1-1-09查询业务场景详情[40]   */
            Assert.isNull(queryBizSceneDetailReq.getBizSceneId(), "D1-1查询行为类型详情-1-1-09查询业务场景详情-业务场景ID不能为空", false);
            bizScene = mBizSceneService.queryBizSceneDetail(queryBizSceneDetailReq);


            bizScene_1 = bizScene;
        }

        QueryBehaviorTypeDetailRespDto retData = new QueryBehaviorTypeDetailRespDto();
        if (behaviorType_1 != null) {
            retData.setBehaviorTypeId(behaviorType_1.getBehaviorTypeId());//SimpleFieldAssign//sourceId:27964_1
            retData.setBehaviorTypeCode(behaviorType_1.getBehaviorTypeCode());//SimpleFieldAssign//sourceId:27965_1
            retData.setBehaviorTypeName(behaviorType_1.getBehaviorTypeName());//SimpleFieldAssign//sourceId:27966_1
            retData.setBehaviorTypeDes(behaviorType_1.getBehaviorTypeDes());//SimpleFieldAssign//sourceId:27967_1
            retData.setBizAppId(behaviorType_1.getBizAppId());//SimpleFieldAssign//sourceId:27968_1
            retData.setBizSceneId(behaviorType_1.getBizSceneId());//SimpleFieldAssign//sourceId:286082_1
            retData.setSubjectLifeCycle(behaviorType_1.getSubjectLifeCycle());//SimpleFieldAssign//sourceId:27969_1
            retData.setOperationInductionId(behaviorType_1.getOperationInductionId());//SimpleFieldAssign//sourceId:27970_1
            retData.setOperateTime(behaviorType_1.getOperateTime());//SimpleFieldAssign//sourceId:27971_1
        }
        if (bizAppScene_1 != null) {
            retData.setBizAppCode(bizAppScene_1.getBizAppCode());//SimpleFieldAssign//sourceId:34692_1
            retData.setBizAppName(bizAppScene_1.getBizAppName());//SimpleFieldAssign//sourceId:34693_1
        }
        if (bizScene_1 != null) {
            retData.setBizSceneCode(bizScene_1.getBizSceneCode());//SimpleFieldAssign//sourceId:286081_1
            retData.setBizSceneName(bizScene_1.getBizSceneName());//SimpleFieldAssign//sourceId:286077_1
        }


        return retData;
    }

    /**
     * D1-1修改行为类型[718]
     * gen by moon at 9/1/2022, 7:20:43 PM
     */
    @Trace(operationName = "D1-1修改行为类型")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Transactional(rollbackFor = Exception.class)
    @Override
    public UpdateBehaviorTypeRespDto updateBehaviorType(UpdateBehaviorTypeReqDto reqDto) {


        //步骤0: 1-1-10修改行为类型 - updateBehaviorType
        boolean bOOLEAN;
        BehaviorType behaviorType = new BehaviorType();
        if (reqDto != null) {
            behaviorType.setBehaviorTypeId(reqDto.getBehaviorTypeId());//sourceId:28030_1
            behaviorType.setBehaviorTypeCode(reqDto.getBehaviorTypeCode());//sourceId:28031_1
            behaviorType.setBehaviorTypeName(reqDto.getBehaviorTypeName());//sourceId:28032_1
            behaviorType.setBehaviorTypeDes(reqDto.getBehaviorTypeDes());//sourceId:28033_1
            behaviorType.setBizAppId(reqDto.getBizAppId());//sourceId:28034_1
            behaviorType.setBizSceneId(reqDto.getBizSceneId());//sourceId:286054_1
            behaviorType.setSubjectLifeCycle(reqDto.getSubjectLifeCycle());//sourceId:28035_1
        }

        /*1-1-10修改行为类型[723]   */
        Assert.isNull(behaviorType.getBehaviorTypeId(), "D1-1修改行为类型-1-1-10修改行为类型-行为类型ID不能为空", false);
        bOOLEAN = mBehaviorTypeService.updateBehaviorType(behaviorType);


        UpdateBehaviorTypeRespDto retData = new UpdateBehaviorTypeRespDto();


        return retData;
    }

    /**
     * D1-1查询行为类型菜单列表(边界)[4338]
     * gen by moon at 10/2/2022, 6:21:06 AM
     */
    @Trace(operationName = "D1-1查询行为类型菜单列表(边界)")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Transactional(rollbackFor = Exception.class)
    @Override
    public QueryBehaviorTypeMenuListBorderRespDto queryBehaviorTypeMenuListBorder(QueryBehaviorTypeMenuListBorderReqDto reqDto) {


        List<BehaviorType> listBehaviorType_1 = new ArrayList<>();
//步骤0: 1-1-09查询业务场景详情 - queryBizSceneDetail
        BizScene bizScene = null;
        QueryBizSceneDetailReq queryBizSceneDetailReq = new QueryBizSceneDetailReq();
        queryBizSceneDetailReq.setSubjectLifeCycle("CURRENTLY_VALID");//sourceId:286583_1
        if (reqDto != null) {
            queryBizSceneDetailReq.setBizSceneCode(reqDto.getBizSceneCode());//SimpleFieldAssign//sourceId:286582_1
        }

        /*1-1-09查询业务场景详情[40]   */
        Assert.isNull(queryBizSceneDetailReq.getBizSceneCode(), "D1-1查询行为类型菜单列表(边界)-1-1-09查询业务场景详情-业务场景标识不能为空", false);
        Assert.isNull(queryBizSceneDetailReq.getSubjectLifeCycle(), "D1-1查询行为类型菜单列表(边界)-1-1-09查询业务场景详情-主体生命周期不能为空", false);
        bizScene = mBizSceneService.queryBizSceneDetail(queryBizSceneDetailReq);


//步骤1: 1-1-10查询行为类型列表 - queryBehaviorTypeList
        List<BehaviorType> listBehaviorType = new ArrayList<>();
        if (bizScene != null) {
            QueryBehaviorTypeListReq queryBehaviorTypeListReq = new QueryBehaviorTypeListReq();
            queryBehaviorTypeListReq.setSubjectLifeCycle("CURRENTLY_VALID");//sourceId:286605_1
            if (bizScene != null) {
                queryBehaviorTypeListReq.setBizSceneId(bizScene.getBizSceneId());//SimpleFieldAssign//sourceId:286604_1
            }

            /*1-1-10查询行为类型列表[4]   */
            Assert.isNull(queryBehaviorTypeListReq.getBizSceneId(), "D1-1查询行为类型菜单列表(边界)-1-1-10查询行为类型列表-关联业务场景ID不能为空", false);
            Assert.isNull(queryBehaviorTypeListReq.getSubjectLifeCycle(), "D1-1查询行为类型菜单列表(边界)-1-1-10查询行为类型列表-主体生命周期不能为空", false);
            listBehaviorType = mBehaviorTypeService.queryBehaviorTypeList(queryBehaviorTypeListReq);


            listBehaviorType_1 = listBehaviorType;
        }

        QueryBehaviorTypeMenuListBorderRespDto retData = new QueryBehaviorTypeMenuListBorderRespDto();
        retData.setBehaviorTypeMenuList(listBehaviorType_1.stream().map(item -> BeanUtil.toBean(item, BehaviorTypeMenuDto.class)).collect(Collectors.toList()));//objList-to-objLists//sourceId:286607_1


        return retData;
    }

    /**
     * D1-1批量查询行为类型(边界)[5003]
     * gen by moon at 10/24/2022, 9:43:54 PM
     */
    @Trace(operationName = "D1-1批量查询行为类型(边界)")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Transactional(rollbackFor = Exception.class)
    @Override
    public BatchQueryBehaviorTypeBorderRespDto batchQueryBehaviorTypeBorder(BatchQueryBehaviorTypeBorderReqDto reqDto) {


        List<BehaviorType> listBehaviorType_1 = new ArrayList<>();
//步骤0: 1-1-10批量查询行为类型 - batchQueryBehaviorType
        List<BehaviorType> listBehaviorType = new ArrayList<>();
        BatchQueryBehaviorTypeReq batchQueryBehaviorTypeReq = new BatchQueryBehaviorTypeReq();
        batchQueryBehaviorTypeReq.setSubjectLifeCycle("CURRENTLY_VALID");//sourceId:388641_1
        if (reqDto != null) {
            batchQueryBehaviorTypeReq.setBehaviorTypeList(reqDto.getBehaviorTypeList());//list-field-assign//sourceId:388640_1
        }

        /*1-1-10批量查询行为类型[5004]   */
        Assert.isNull(batchQueryBehaviorTypeReq.getSubjectLifeCycle(), "D1-1批量查询行为类型(边界)-1-1-10批量查询行为类型-主体生命周期不能为空", false);
        listBehaviorType = mBehaviorTypeService.batchQueryBehaviorType(batchQueryBehaviorTypeReq);


        listBehaviorType_1 = listBehaviorType;

        BatchQueryBehaviorTypeBorderRespDto retData = new BatchQueryBehaviorTypeBorderRespDto();
        retData.setBehaviorTypeList(listBehaviorType_1.stream().map(item -> BeanUtil.toBean(item, BehaviorTypeDto.class)).collect(Collectors.toList()));//objList-to-objLists//sourceId:388644_1


        return retData;
    }
    //
}
